/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import javax.microedition.lcdui.Canvas;

/**
 *
 * @author kien.tran
 */
public class SoftKey {
    public static final String PLATFORM_MOTOROLA = "motorola";
    public static final String PLATFORM_NOKIA = "nokia";
    public static final String PLATFORM_SONY_ERICSSON = "SE";
    public static final String PLATFORM_SIEMENS = "siemens";
    public static final String PLATFORM_SAMSUNG = "samsung";
    public static final String PLATFORM_LG = "LG";
    public static final String PLATFORM_NOT_DEFINED = "NA";


    private static final int SOFT_KEY_LEFT_MOTOROLA = -21;
    private static final int SOFT_KEY_RIGHT_MOTOROLA = -22;
    private static final int SOFT_KEY_LEFT_MOTOROLA2 = -20;
    private static final int SOFT_KEY_LEFT_MOTOROLA1 = 21;
    private static final int SOFT_KEY_RIGHT_MOTOROLA1 = 22;
    private static final int SOFT_KEY_LEFT_SAMSUNG = -6;
    private static final int SOFT_KEY_RIGHT_SAMSUNG = -7;
    private static final int SOFT_KEY_LEFT_SIEMENS = -1;
    private static final int SOFT_KEY_RIGHT_SIEMENS = -4;
    private static final int SOFT_KEY_LEFT_NOKIA = -6;
    private static final int SOFT_KEY_RIGHT_NOKIA = -7;
    private static final int SOFT_KEY_LEFT_SE = -6;
    private static final int SOFT_KEY_RIGHT_SE = -7;
    private static final String SOFT_WORD = "SOFT";

    public static int getLeftSoftkeyCode(Canvas canvas) {
        String platform = getPlatform(canvas);
        int keyCode = 0;
        try {
            if (platform.equals(PLATFORM_MOTOROLA)) {
                String softkeyLeftMoto = "";
                try {
                    softkeyLeftMoto = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                String softkeyLeftMoto1 = "";
                try {
                    softkeyLeftMoto1 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                String softkeyLeftMoto2 = "";
                try {
                    softkeyLeftMoto2 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("1") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA;
                } else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("1") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA1;
                } else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("1") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA2;
                } else if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("LEFT") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA;
                } else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("LEFT") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA1;
                } else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("LEFT") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA2;
                }

            } else if (platform.equals(PLATFORM_NOKIA)) {
                return SOFT_KEY_LEFT_NOKIA;
            } else if (platform.equals(PLATFORM_SAMSUNG)) {
                return SOFT_KEY_LEFT_SAMSUNG;
            } else if (platform.equals(PLATFORM_SIEMENS)) {
                String leftKeySiemensName = canvas.getKeyName(SOFT_KEY_LEFT_SIEMENS).toUpperCase();
                if (leftKeySiemensName.indexOf(SOFT_WORD) >= 0) {
                    if (leftKeySiemensName.indexOf("1") >= 0) {
                        return SOFT_KEY_LEFT_SIEMENS;
                    } else if (leftKeySiemensName.indexOf("LEFT") >= 0) {
                        return SOFT_KEY_LEFT_SIEMENS;
                    }
                }
            } else if (platform.equals(PLATFORM_SONY_ERICSSON)) {
                return SOFT_KEY_LEFT_SE;
            } else if (platform.equals(PLATFORM_NOT_DEFINED)) {
                for (int i = -125; i <= 125; i++) {
                    if (i == 0) {
                        i++;
                    }
                    final String s = canvas.getKeyName(i).toUpperCase();
                    if (s.indexOf(SOFT_WORD) >= 0) {
                        if (s.indexOf("1") >= 0) {
                            keyCode = i;
                            break;
                        }
                        if (s.indexOf("LEFT") >= 0) {
                            keyCode = i;
                            break;
                        }
                    }
                }
            }
        } catch (Throwable iaEx) {
        }
        return keyCode;
    }

    public static int getRightSoftkeyCode(Canvas canvas) {
        String platform = getPlatform(canvas);
        int keyCode = 0;
        try {
            if (platform.equals(PLATFORM_MOTOROLA)) {

                String rightSoftMoto1 = "";
                try {
                    rightSoftMoto1 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                String rightSoftMoto = "";
                try {
                    rightSoftMoto = canvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                String rightSoftMoto2 = "";
                try {
                    rightSoftMoto2 = canvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA1).toUpperCase();
                } catch (IllegalArgumentException ilae) {
                }
                if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("2") >= 0) {
                    return SOFT_KEY_RIGHT_MOTOROLA;
                } else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("2") >= 0) {
                    return SOFT_KEY_RIGHT_MOTOROLA;
                } else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("2") >= 0) {
                    return SOFT_KEY_RIGHT_MOTOROLA1;
                } else if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("RIGHT") >= 0) {
                    return SOFT_KEY_LEFT_MOTOROLA;
                } else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("RIGHT") >= 0) {
                    return SOFT_KEY_RIGHT_MOTOROLA1;
                } else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("RIGHT") >= 0) {
                    return SOFT_KEY_RIGHT_MOTOROLA;
                }

            } else if (platform.equals(PLATFORM_NOKIA)) {
                return SOFT_KEY_RIGHT_NOKIA;
            } else if (platform.equals(PLATFORM_SAMSUNG)) {
                return SOFT_KEY_RIGHT_SAMSUNG;
            } else if (platform.equals(PLATFORM_SIEMENS)) {
                String rightSoftSiemens = canvas.getKeyName(SOFT_KEY_RIGHT_SIEMENS).toUpperCase();
                if (rightSoftSiemens.indexOf(SOFT_WORD) >= 0) {
                    if (rightSoftSiemens.indexOf("4") >= 0) {
                        return SOFT_KEY_RIGHT_SIEMENS;
                    } else if (rightSoftSiemens.indexOf("RIGHT") >= 0) {
                        return SOFT_KEY_RIGHT_SIEMENS;
                    }
                }
            } else if (platform.equals(PLATFORM_SONY_ERICSSON)) {
                return SOFT_KEY_RIGHT_SE;
            } else if (platform.equals(PLATFORM_NOT_DEFINED)) {
                for (int i = -125; i <= 125; i++) {
                    if (i == 0) {
                        i++;
                    }
                    String keyName = canvas.getKeyName(i).toUpperCase();
                    if (keyName.indexOf(SOFT_WORD) >= 0) {
                        if (keyName.indexOf("2") >= 0) {
                            keyCode = i;
                            break;
                        } else if (keyName.indexOf("4") >= 0) {
                            keyCode = i;
                            break;
                        } else if (keyName.indexOf("RIGHT") >= 0) {
                            keyCode = i;
                            break;
                        }
                    }
                }
            }
        } catch (Throwable iaEx) {
        }
        return keyCode;
    }

    private static String getPlatform(Canvas canvas) {
        // detecting NOKIA or SonyEricsson
        try {
            final String currentPlatform = System.getProperty("microedition.platform");
            if (currentPlatform.indexOf("Nokia") != -1) {
                return PLATFORM_NOKIA;
            } else if (currentPlatform.indexOf("SonyEricsson") != -1) {
                return PLATFORM_SONY_ERICSSON;
            }
        } catch (Throwable ex) {
        }
        // detecting SAMSUNG
        try {
            Class.forName("com.samsung.util.Vibration");
            return PLATFORM_SAMSUNG;
        } catch (Throwable ex) {
        }
        // detecting MOTOROLA
        try {
            Class.forName("com.motorola.multimedia.Vibrator");
            return PLATFORM_MOTOROLA;
        } catch (Throwable ex) {
            try {
                Class.forName("com.motorola.graphics.j3d.Effect3D");
                return PLATFORM_MOTOROLA;
            } catch (Throwable ex2) {
                try {
                    Class.forName("com.motorola.multimedia.Lighting");
                    return PLATFORM_MOTOROLA;
                } catch (Throwable ex3) {
                    try {
                        Class.forName("com.motorola.multimedia.FunLight");
                        return PLATFORM_MOTOROLA;
                    } catch (Throwable ex4) {
                    }
                }
            }
        }
        try {
            if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase().indexOf(SOFT_WORD) > -1) {
                return PLATFORM_MOTOROLA;
            }
        } catch (Throwable e) {
            try {
                if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase().indexOf(SOFT_WORD) > -1) {
                    return PLATFORM_MOTOROLA;
                }
            } catch (Throwable e1) {
                try {
                    if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase().indexOf(SOFT_WORD) > -1) {
                        return PLATFORM_MOTOROLA;
                    }
                } catch (Throwable e2) {
                }
            }
        }
        // detecting SIEMENS
        try {
            Class.forName("com.siemens.mp.io.File");
            return PLATFORM_SIEMENS;
        } catch (Throwable ex) {
        }
        // detecting LG
        try {
            Class.forName("mmpp.media.MediaPlayer");
            return PLATFORM_LG;
        } catch (Throwable ex) {
            try {
                Class.forName("mmpp.phone.Phone");
                return PLATFORM_LG;
            } catch (Throwable ex1) {
                try {
                    Class.forName("mmpp.lang.MathFP");
                    return PLATFORM_LG;
                } catch (Throwable ex2) {
                    try {
                        Class.forName("mmpp.media.BackLight");
                        return PLATFORM_LG;
                    } catch (Throwable ex3) {
                    }
                }
            }
        }
        return PLATFORM_NOT_DEFINED;
    }
}
